/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
module dwt.widgets.canvas;


private import dwt.dwt;


private import dwt.graphics.font;
private import dwt.graphics.rectangle;
private import dwt.internal.win32.os;
private import dwt.util.util;
private import dwt.widgets.caret;
public import dwt.widgets.composite;
private import dwt.widgets.control;
private import dwt.widgets.runnablelock;
private import dwt.widgets.typedlistener;
private import dwt.widgets.widget;


/**
 * Instances of this class provide a surface for drawing
 * arbitrary graphics.
 * <dl>
 * <dt><b>Styles:</b></dt>
 * <dd>(none)</dd>
 * <dt><b>Events:</b></dt>
 * <dd>(none)</dd>
 * </dl>
 * <p>
 * This class may be subclassed by custom control implementors
 * who are building controls that are <em>not</em> constructed
 * from aggregates of other controls. That is, they are either
 * painted using DWT graphics calls or are handled by native
 * methods.
 * </p>
 *
 * @see Composite
 */

public class Canvas : Composite {
	Caret caret;
	
/**
 * Prevents uninitialized instances from being created outside the package.
 */
this () {
}

/**
 * Constructs a new instance of this class given its parent
 * and a style value describing its behavior and appearance.
 * <p>
 * The style value is either one of the style constants defined in
 * class <code>DWT</code> which is applicable to instances of this
 * class, or must be built by <em>bitwise OR</em>'ing together 
 * (that is, using the <code>int</code> "|" operator) two or more
 * of those <code>DWT</code> style constants. The class description
 * lists the style constants that are applicable to the class.
 * Style bits are also inherited from superclasses.
 * </p>
 *
 * @param parent a composite control which will be the parent of the new instance (cannot be null)
 * @param style the style of control to construct
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
 *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
 * </ul>
 *
 * @see DWT
 * @see Widget#checkSubclass
 * @see Widget#getStyle
 */
public this (Composite parent, int style) {
	super (parent, style);
}

/*
* Not currently used.
*/
void clearArea (int x, int y, int width, int height) {
	checkWidget ();
	if (OS.IsWindowVisible (handle)) return;
	RECT rect;
	OS.SetRect (&rect, x, y, x + width, y + height);
	HDC hDC = OS.GetDCEx (handle, null, OS.DCX_CACHE | OS.DCX_CLIPCHILDREN | OS.DCX_CLIPSIBLINGS);
	drawBackground (hDC, &rect);
	OS.ReleaseDC (handle, hDC);
}

/**
 * Returns the caret.
 * <p>
 * The caret for the control is automatically hidden
 * and shown when the control is painted or resized,
 * when focus is gained or lost and when an the control
 * is scrolled.  To avoid drawing on top of the caret,
 * the programmer must hide and show the caret when
 * drawing in the window any other time.
 * </p>
 *
 * @return the caret
 *
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 */
public Caret getCaret () {
	checkWidget ();
	return caret;
}

void releaseWidget () {
	if (caret !is null) caret.releaseResources ();
	caret = null;
	super.releaseWidget ();
}

/**
 * Scrolls a rectangular area of the receiver by first copying 
 * the source area to the destination and then causing the area
 * of the source which is not covered by the destination to
 * be repainted. Children that intersect the rectangle are
 * optionally moved during the operation. In addition, outstanding
 * paint events are flushed before the source area is copied to
 * ensure that the contents of the canvas are drawn correctly.
 *
 * @param destX the x coordinate of the destination
 * @param destY the y coordinate of the destination
 * @param x the x coordinate of the source
 * @param y the y coordinate of the source
 * @param width the width of the area
 * @param height the height of the area
 * @param all <code>true</code>if children should be scrolled, and <code>false</code> otherwise
 *
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 */
public void scroll (int destX, int destY, int x, int y, int width, int height, boolean all) {
	checkWidget ();
	forceResize ();
	boolean isFocus = caret !is null && caret.isFocusCaret ();
	if (isFocus) caret.killFocus ();
	RECT sourceRect;
	OS.SetRect (&sourceRect, x, y, x + width, y + height);
	RECT clientRect;
	OS.GetClientRect (handle, &clientRect);
	if (OS.IntersectRect (&clientRect, &sourceRect, &clientRect)) {
		version(WinCE) {
			OS.UpdateWindow (handle);
		} else {
			int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN;
			OS.RedrawWindow (handle, null, null, flags);
		}
	}
	int deltaX = destX - x, deltaY = destY - y;
	int flags = OS.SW_INVALIDATE | OS.SW_ERASE;
	/*
	* Feature in Windows.  If any child in the widget tree partially
	* intersects the scrolling rectangle, Windows moves the child
	* and copies the bits that intersect the scrolling rectangle but
	* does not redraw the child.
	* 
	* Feature in Windows.  When any child in the widget tree does
	* not intersect the scrolling rectangle but the parent does intersect,
	* Windows does not move the child.  This is the documented (but
	* strange) Windows behavior.
	* 
	* The fix is to not use SW_SCROLLCHILDREN and move the children
	* explicitly after scrolling.  
	*/
//	if (all) flags |= OS.SW_SCROLLCHILDREN;
	OS.ScrollWindowEx (handle, deltaX, deltaY, &sourceRect, null, null, null, flags);
	if (all) {
		Control [] children = _getChildren ();
		for (int i=0; i<children.length; i++) {
			Control child = children [i];
			Rectangle rect = child.getBounds ();
			if (Math.min(x + width, rect.x + rect.width) >= Math.max (x, rect.x) && 
				Math.min(y + height, rect.y + rect.height) >= Math.max (y, rect.y)) {
					child.setLocation (rect.x + deltaX, rect.y + deltaY);
			}
		}
	}
	if (isFocus) caret.setFocus ();
}

/**
 * Sets the receiver's caret.
 * <p>
 * The caret for the control is automatically hidden
 * and shown when the control is painted or resized,
 * when focus is gained or lost and when an the control
 * is scrolled.  To avoid drawing on top of the caret,
 * the programmer must hide and show the caret when
 * drawing in the window any other time.
 * </p>
 * @param caret the new caret for the receiver, may be null
 *
 * @exception IllegalArgumentException <ul>
 *    <li>ERROR_INVALID_ARGUMENT - if the caret has been disposed</li>
 * </ul>
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 */
public void setCaret (Caret caret) {
	checkWidget ();
	Caret newCaret = caret;
	Caret oldCaret = this.caret;
	this.caret = newCaret;
	if (isFocusControl ()) {
		if (oldCaret !is null) oldCaret.killFocus ();
		if (newCaret !is null) {
			if (newCaret.isDisposed()) error(__FILE__, __LINE__, DWT.ERROR_INVALID_ARGUMENT);
			newCaret.setFocus ();
		}
	}
}

public void setFont (Font font) {
	checkWidget ();
	if (caret !is null) caret.setFont (font);
	super.setFont (font);
}

LDWTRESULT WM_KILLFOCUS_ (int wParam, int lParam) {
	LDWTRESULT result  = super.WM_KILLFOCUS_ (wParam, lParam);
	if (caret !is null) caret.killFocus ();
	return result;
}

LDWTRESULT WM_SETFOCUS_ (int wParam, int lParam) {
	LDWTRESULT result  = super.WM_SETFOCUS_ (wParam, lParam);
	if (caret !is null) caret.setFocus ();
	return result;
}

LDWTRESULT WM_WINDOWPOSCHANGED_ (int wParam, int lParam) {
	LDWTRESULT result  = super.WM_WINDOWPOSCHANGED_ (wParam, lParam);
	if (result !is null) return result;
	/*
	* Bug in Windows.  When a window with style WS_EX_LAYOUTRTL
	* that contains a caret is resized, Windows does not move the
	* caret in relation to the mirrored origin in the top right.
	* The fix is to hide the caret in WM_WINDOWPOSCHANGING and
	* show the caret in WM_WINDOWPOSCHANGED.
	*/
	boolean isFocus = (style & DWT.RIGHT_TO_LEFT) != 0 && caret !is null && caret.isFocusCaret ();
	if (isFocus) caret.setFocus ();
	return result;
}

LDWTRESULT WM_WINDOWPOSCHANGING_ (int wParam, int lParam) {
	LDWTRESULT result  = super.WM_WINDOWPOSCHANGING_ (wParam, lParam);
	if (result !is null) return result;
	/*
	* Bug in Windows.  When a window with style WS_EX_LAYOUTRTL
	* that contains a caret is resized, Windows does not move the
	* caret in relation to the mirrored origin in the top right.
	* The fix is to hide the caret in WM_WINDOWPOSCHANGING and
	* show the caret in WM_WINDOWPOSCHANGED.
	*/
	boolean isFocus = (style & DWT.RIGHT_TO_LEFT) != 0 && caret !is null && caret.isFocusCaret ();
	if (isFocus) caret.killFocus ();
	return result;
}

}
